#include "jtoolunitbox.h"
#include "ui_jtoolunitbox.h"

namespace QTE
{

class Q_DECL_HIDDEN JToolUnitBoxPrivate : public QObject
{
public:
	explicit JToolUnitBoxPrivate(JToolUnitBox *q_ptr);
	~JToolUnitBoxPrivate();

public:
	void on_checkBoxClicked(bool checked);
	void on_expandButtonClicked();

public:
	JToolUnitBox *q_ptr;
	Ui::JToolUnitBox *ui;
	QHBoxLayout *m_layout;
	QWidget *m_widget = nullptr;
};

JToolUnitBoxPrivate::JToolUnitBoxPrivate(JToolUnitBox *q_ptr) :
	QObject(q_ptr),
	q_ptr(q_ptr),
	ui(new Ui::JToolUnitBox()),
	m_layout(new QHBoxLayout())
{
	ui->setupUi(q_ptr);
	m_layout->setContentsMargins(0,0,0,0);

#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
	m_layout->setMargin(0);
#endif //qt6

	ui->toolButton->setEnabled(false);
	ui->widget->setLayout(m_layout);
	ui->widget->hide();
}

JToolUnitBoxPrivate::~JToolUnitBoxPrivate()
{
	delete ui;
}

void JToolUnitBoxPrivate::on_checkBoxClicked(bool checked)
{
	q_ptr->setChecked(checked);
}

void JToolUnitBoxPrivate::on_expandButtonClicked()
{
	q_ptr->setExpand(not q_ptr->isExpand());
}

/*--------------------------------------------------------------------------------------------------*/

JToolUnitBox::JToolUnitBox(QWidget *parent) :
	QFrame(parent),
	d_ptr(new JToolUnitBoxPrivate(this))
{
	d_ptr->ui->titleLabel->installEventFilter(this);
	connect(d_ptr->ui->checkBox, &QCheckBox::clicked, d_ptr, &JToolUnitBoxPrivate::on_checkBoxClicked);
	connect(d_ptr->ui->toolButton, &QToolButton::clicked, d_ptr, &JToolUnitBoxPrivate::on_expandButtonClicked);
}

JToolUnitBox::JToolUnitBox(const QString &title, QWidget *parent) :
	JToolUnitBox(parent)
{
	setBoxTitle(title);
}

JToolUnitBox::~JToolUnitBox()
{

}

void JToolUnitBox::setBoxTitle(const QString &text)
{
	d_ptr->ui->titleLabel->setText(text);
}

void JToolUnitBox::setWidget(QWidget *widget, bool deleteOld)
{
	if( d_ptr->m_widget and deleteOld )
		d_ptr->m_widget->deleteLater();

	d_ptr->m_widget = widget;

	if( widget )
		d_ptr->m_layout->addWidget(widget);
}

void JToolUnitBox::setCheckable(bool enable)
{
	d_ptr->ui->checkBox->setVisible(enable);
	d_ptr->ui->checkBox->setChecked(not enable);
	d_ptr->ui->toolButton->setEnabled(not enable);
}

void JToolUnitBox::setChecked(bool check)
{
	if( not isCheckable() )
		return ;

	d_ptr->ui->checkBox->setChecked(check);
	d_ptr->ui->toolButton->setEnabled(check);

	if( not check )
		collapse();
}

void JToolUnitBox::setExpand(bool expand)
{
	if( expand )
	{
		if( isCheckable() and not isChecked() )
			return ;

		d_ptr->ui->toolButton->setArrowType(Qt::DownArrow);
		d_ptr->ui->widget->show();
	}
	else
	{
		d_ptr->ui->toolButton->setArrowType(Qt::LeftArrow);
		d_ptr->ui->widget->hide();
	}
}

QString JToolUnitBox::boxTitle() const
{
	return d_ptr->ui->titleLabel->text();
}

const QWidget *JToolUnitBox::widget() const
{
	return d_ptr->m_widget;
}

QWidget *JToolUnitBox::widget()
{
	return d_ptr->m_widget;
}

bool JToolUnitBox::isCheckable() const
{
	return d_ptr->ui->checkBox->isVisible();
}

bool JToolUnitBox::isChecked() const
{
	return isCheckable()? d_ptr->ui->checkBox->isChecked() : false;
}

bool JToolUnitBox::isExpand() const
{
	return d_ptr->ui->widget->isVisible();
}

bool JToolUnitBox::eventFilter(QObject *watched, QEvent *event)
{
	if( watched != d_ptr->ui->titleLabel )
		return false;

	else if( event->type() != QEvent::MouseButtonDblClick )
		return false;

	d_ptr->on_expandButtonClicked();
	return false;
}

} //namespace QTE
